package com.ruoyi.framework.web.service;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.GiveType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqDynamicGive;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.service.IWqDynamicGiveService;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqNoticeGiveService;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMsgService;
import cn.hutool.core.date.DateUtil;

@Component
public class DynamicGiveService {
	
	@Autowired
    private ISysUserService sysUserService;
	
	@Autowired
	private IWqDynamicService wqDynamicService;
	
	@Autowired
	private IWqDynamicGiveService wqDynamicGiveService;
	
	@Autowired
	private IWqNoticeGiveService wqNoticeGiveService;
	
	@Autowired
    private WxMsgService wxMsgService;

	public Boolean insertWqDynamicGive(WqDynamicGive wqDynamicGive){
    	Boolean isGvie = false;
    	SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
    	WqDynamic dynamic = wqDynamicService.getById(wqDynamicGive.getDynamicId());
    	if(sysUser.getUserId().equals(dynamic.getUserId())){
    		// 不能给自己点赞
    		return true;
    	}
    	WqDynamicGive give = getWqDynamicGive(wqDynamicGive.getDynamicId(), sysUser.getUserId());
    	if(give == null){
    		isGvie = true;
    		wqDynamicGive.setUserId(sysUser.getUserId());
        	wqDynamicGive.setAvatar(sysUser.getAvatar());
        	wqDynamicGive.setNickName(sysUser.getNickName());
        	wqDynamicGive.setPhone(sysUser.getPhonenumber());
        	wqDynamicGive.setSex(sysUser.getSex());
        	wqDynamicGiveService.save(wqDynamicGive);
        	
        	if("0".equals(dynamic.getIsParty())){
        		// 点赞通知
            	WqNoticeGive noticeGive = new WqNoticeGive();
            	noticeGive.setDynamicId(dynamic.getId());
            	if(StringUtils.isNotBlank(dynamic.getImage())){
            		noticeGive.setDynamicImage(dynamic.getImage().split(",")[0]);
            	}
            	noticeGive.setGiveType(GiveType.DYNAMIC_GIVE.getCode());
            	
            	noticeGive.setUserId(sysUser.getUserId());
            	noticeGive.setAvatar(sysUser.getAvatar());
            	noticeGive.setNickName(sysUser.getNickName());
            	noticeGive.setPhone(sysUser.getPhonenumber());
            	noticeGive.setSex(sysUser.getSex());
            	noticeGive.setNoticeUserId(dynamic.getUserId());
            	wqNoticeGiveService.save(noticeGive);
            	
            	// 发送微信订阅消息
            	SysUser user = sysUserService.getById(dynamic.getUserId());
                if(StringUtils.isNotBlank(user.getAppId())){
                	if(StringUtils.isNotBlank(user.getWxOpenId())){
                		WxMpMsgForm msgForm = new WxMpMsgForm();
                		msgForm.setWxId(user.getWxId());
                		msgForm.setAppId(user.getAppId());
                		msgForm.setOpenId(user.getWxOpenId());
                		Map<String,String> dataMap = new HashMap<>();
                		dataMap.put("first", "您好，您收到一条动态点赞");
                		dataMap.put("keyword1", sysUser.getNickName());
                		dataMap.put("keyword2", DateUtil.now());
                		dataMap.put("keyword3", "您有一个新的点赞");
                		dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
                		msgForm.setDataMap(dataMap);
                		msgForm.setPage("pages/index");
                		wxMsgService.sendTemplateMsg(msgForm);
                	}
                }
        	}
        	
    	}else{
    		isGvie = false;
    		wqDynamicGiveService.removeById(give.getId());
    		wqNoticeGiveService.deleteByDynamicIdAndUserId(dynamic.getId(), sysUser.getUserId());
    	}
    	// 统计动态点赞数量
    	Integer giveNum = getGiveNumByDynamicId(wqDynamicGive.getDynamicId());
    	
    	// 更新动态点赞数量
    	dynamic.setGiveNum(giveNum);
    	wqDynamicService.updateById(dynamic);
    	
    	// 更新用户未读消息数量
    	if("0".equals(dynamic.getIsParty())){
    		SysUser user = sysUserService.getById(dynamic.getUserId());
        	Integer giveNewNum = wqNoticeGiveService.getUnReadNum(dynamic.getUserId());
        	user.setGiveNewNum(giveNewNum);
        	sysUserService.updateById(user);
    	}
    	return isGvie;
    }
	
	private WqDynamicGive getWqDynamicGive(String dynamicId, Long userId){
    	QueryWrapper<WqDynamicGive> qw = new QueryWrapper<>();
    	WqDynamicGive entity = new WqDynamicGive();
    	entity.setDynamicId(dynamicId);
    	entity.setUserId(userId);
    	qw.setEntity(entity);
    	return wqDynamicGiveService.getOne(qw);
    }
	
	private Integer getGiveNumByDynamicId(String dynamicId){
    	QueryWrapper<WqDynamicGive> qw = new QueryWrapper<>();
    	WqDynamicGive entity = new WqDynamicGive();
    	entity.setDynamicId(dynamicId);
    	entity.setStatus("0");
    	qw.setEntity(entity);
    	return wqDynamicGiveService.count(qw);
    }
	
}
